//
//  HXRegisterViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/19.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXRegisterViewController.h"
#import "HX_Macro.h"
#import "UIImageView+AFNetworking.h"
@interface HXRegisterViewController ()
@property (nonatomic, copy) NSString *verString;
@property (nonatomic, copy) NSString *session;
@end

@implementation HXRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getPictureImg];
    [self.getIdentry addTarget:self action:@selector(get:) forControlEvents:UIControlEventTouchUpInside];
    [self.changeBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [self.backBtn addTarget:self action:@selector(backView:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)backView:(UIButton *)btn {
    [self dismissViewControllerAnimated:YES completion:^{ 
    }];
}
- (void)change:(UIButton *)btn {
    [self getPictureImg];
}
- (void)getPictureImg {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"methodName=UserVerify&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        self.verString = result[@"data"][@"sessid"];
        [self.identifyingImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://m.izhangchu.com/index/verify.html?sessid=%@",[self encodeString:self.verString]]]];
    } Failure:^(NSError *error) {
    }];
    self.session = [self encodeString:self.verString];
    
}
- (NSString *)encodeString:(NSString *)unencodedString {
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)unencodedString,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
    return encodedString;
}
- (void)get:(UIButton *)btn {
    [self getIdentry:self.textPic.text textTelephone:self.textTelePhone.text];
}
- (void)getIdentry:(NSString *)textPic textTelephone:(NSString *)telePhone {
//    NSString *url = @"http://api.izhangchu.com";
//    NSString *body = [NSString stringWithFormat:@"sessid=%%7Bf1c8c16e-a179-16f2-67b9-733391e2c516%%7D&device_id=100d8559094fe4cba38&methodName=UserLogin&verify=%@&mobile=%@&version=4.40&", textPic, telePhone];
//    //    NSLog(@"%@ , %@", body, _shuru.text);
//    NSDictionary *dicHearder = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com", @"Connection" : @"Keep-Alive"};
//    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHearder Response:HXJSON Success:^(id result) {
//        NSLog(@"%@", result);
//    } Failure:^(NSError *error) {
//    }];
    
    NSString *bodyString = [NSString stringWithFormat:@"sessid=%@&device_id=190e35f7e04db0d27cf&methodName=UserLogin&verify=%@&mobile=%@&version=4.40&", self.session, self.textPic.text, self.textTelePhone.text];
    [HXNetTool postNetWithUrl:@"http://api.izhangchu.com/" body:bodyString BodyStyle:HXBodyString Header:@{@"Content-Type":@"application/x-www-form-urlencoded"} Response:HXJSON Success:^(id result) {
        
        NSLog(@"%@", result);
    } Failure:^(NSError *error) {
        
        
    }];
    
    
}
@end
